package com.sky.service.impl;


import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.CateTypeConstant;
import com.sky.dto.CateDTO;
import com.sky.dto.PageQuery;
import com.sky.entity.Category;
import com.sky.entity.FirstCategory;
import com.sky.entity.SecondCategory;
import com.sky.entity.ThirdCategory;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.vo.FirstCategoryVO;
import com.sky.vo.SecondCategoryVO;
import com.sky.vo.ThirdCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements IService<Category> {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 获取分类数据
     *
     * @return
     */
    public List<FirstCategory> categories() {
        // 获取所有分类数据
        List<FirstCategory> firstCategories = categoryMapper.getFirstCategoryList();
        List<SecondCategory> secondCategories = categoryMapper.getSecondCategoryList();
        List<ThirdCategory> thirdCategories = categoryMapper.getThirdCategoryList();

        // 构建分类映射关系
        Map<Integer, FirstCategory> firstMap = firstCategories.stream()
                .peek(f -> f.setChildren(new ArrayList<>()))
                .collect(Collectors.toMap(FirstCategory::getId, Function.identity()));

        Map<Integer, SecondCategory> secondMap = secondCategories.stream()
                .peek(s -> s.setChildren(new ArrayList<>()))
                .collect(Collectors.toMap(SecondCategory::getId, Function.identity()));

        // 构建分类树结构
        secondCategories.forEach(second ->
                Optional.ofNullable(firstMap.get(second.getFId()))
                        .ifPresentOrElse(
                                first -> first.getChildren().add(second),
                                () -> log.warn("一级分类不存在")
                        )
        );

        thirdCategories.forEach(third ->
                Optional.ofNullable(secondMap.get(third.getSId()))
                        .ifPresentOrElse(
                                second -> second.getChildren().add(third),
                                () -> log.warn("二级分类不存在")
                        )
        );

        return firstCategories;
    }

    /**
     * 管理端获取一级分类
     *
     * @param pageQuery
     * @return
     */
    public PageResult first(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getSize());
        Page<FirstCategoryVO> first = categoryMapper.first();
        return new PageResult(first.getTotal(), first.getResult());
    }

    /**
     * 管理端获取二级分类
     *
     * @param pageQuery
     * @return
     */
    public PageResult second(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getSize());
        Page<SecondCategoryVO> second = categoryMapper.second();
        return new PageResult(second.getTotal(), second.getResult());
    }

    /**
     * 管理管获取三级分类
     *
     * @param pageQuery
     * @return
     */
    public PageResult third(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getSize());
        Page<ThirdCategoryVO> third = categoryMapper.third();
        return new PageResult(third.getTotal(), third.getResult());
    }

    /**
     * 获取分类详情
     *
     * @param type
     * @param id
     * @return
     */
    public Category detail(Integer type, Integer id) {
        if (type == CateTypeConstant.FIRST_LEVEL) {
            return categoryMapper.firstDetail(id);
        } else if (type == CateTypeConstant.SECOND_LEVEL) {
            return categoryMapper.secondDetail(id);
        } else {
            return categoryMapper.thirdDetail(id);
        }
    }

    /**
     * 修改分类
     *
     * @param cateDTO
     */
    public void updateByType(CateDTO cateDTO) {
        Integer type = cateDTO.getType();
        if (type == CateTypeConstant.FIRST_LEVEL) {
            categoryMapper.firstUpdate(cateDTO);
        } else if (type == CateTypeConstant.SECOND_LEVEL) {
            categoryMapper.secondUpdate(cateDTO);
        } else {
            categoryMapper.thirdUpdate(cateDTO);
        }
    }

    /**
     * 删除分类
     *
     * @param type
     * @param id
     */
    @Transactional
    public void deleteByType(Integer type, Integer id) {
        if (type == CateTypeConstant.FIRST_LEVEL) {
            categoryMapper.firstDelete(id);
        } else if (type == CateTypeConstant.SECOND_LEVEL) {
            categoryMapper.secondDelete(id);
        } else {
            categoryMapper.thirdDelete(id);
        }
    }

    /**
     * 添加分类
     * @param cateDTO
     */
    public void saveByType(CateDTO cateDTO) {
        Integer type = cateDTO.getType();
        if (type == CateTypeConstant.FIRST_LEVEL) {
            categoryMapper.firstSave(cateDTO);
        } else if (type == CateTypeConstant.SECOND_LEVEL) {
            categoryMapper.secondSave(cateDTO);
        } else {
            categoryMapper.thirdSave(cateDTO);
        }
    }
}